﻿using System.Collections.Generic;
using System.Linq;
using Data.Objects.Player;
using DMHelper.Logic.Extensions;
using Data.Objects.Room;

namespace DMHelper.Logic.DMLogic
{
    public class DMLogic
    {
        public DMLogic()
        {
        }


        public void RollInitiative(IOrderedEnumerable<IPlayerCharacter> players)
        {
            foreach (var playerCharacter in players)
            {
                playerCharacter.RollInitiative();
                playerCharacter.Active = true;
            }
        }

        
        public IOrderedEnumerable<IPlayerCharacter> GetPlayers()
        {
            var players = Data.PlayerData.PlayerRepository.GetPlayers();
            return players.Select(x => x.ConvertToPlayerLogic()).ToList<IPlayerCharacter>().OrderByDescending(x=>x.Initiative);
        }



        public IOrderedEnumerable<IRoom> GetRooms()
        {
            var rooms = Data.RoomData.RoomRepository.GetRooms();
            return rooms.Select(x => x.ConvertToRoomLogic()).ToList<IRoom>().OrderByDescending(x => x.ToString());
        }

        public void EndTurn(IOrderedEnumerable<IPlayerCharacter> players, IPlayerCharacter activePlayer)
        {
            activePlayer.Active = false;

            List<IPlayerCharacter> activePlayers = players.Where(x => x.Active).ToList();
            if(activePlayers.Count == 0)
            {
                foreach (var playerCharacter in players)
                {
                    playerCharacter.Active = true;
                }
            }
        }
    }
}